<template>
  <div class="edit-container">
    <el-form v-loading="formLoading" ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="封面" prop="goodsFace">
        <div style="display: flex;align-items: center;">
          <el-image v-if="form.goodsFace" :src="imageUrl+form.goodsFace" class="img"
                    referrerpolicy="no-referrer"></el-image>
          <el-button type="primary" @click="uploads.show=true" size="mini">选择图片</el-button>
        </div>
      </el-form-item>
      <el-form-item label="图片" prop="file">
        <div class="imgList">
          <template v-for="item in form.file">
            <el-image v-if="item.fileType === 1" :key="item.id" :src="imageUrl + item.url" class="img"
                      referrerpolicy="no-referrer"></el-image>
            <video v-else-if="item.fileType === 2" :key="item.id" :src="imageUrl +item.url" controls width="120" height="120"></video>
          </template>
        </div>
        <el-button type="primary" @click="imgUploads.show=true" size="mini">选择图片</el-button>
      </el-form-item>
      <el-row :gutter="10" align="middle">
        <el-col :span="10">
          <el-form-item label="商品名称" prop="name">
            <el-input type="text" placeholder="商品名称" v-model="form.name" class="col" maxlength="20"/>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="库存" prop="number">
            <el-input type="number" placeholder="库存" v-model="form.number" class="col" maxlength="2000"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10" align="middle">
        <el-col :span="10">
          <el-form-item label="所需积分" prop="integral">
            <el-input type="number" placeholder="积分" v-model="form.integral" class="col" maxlength="6"/>
          </el-form-item>
        </el-col>
        <el-col :span="14">
        <el-form-item label="所在区域" prop="value">
          <el-cascader class="col"
                       placeholder="可输入地区名称进行搜索"
                       v-model="form.value"
                       :options="options"
                       clearable
                       :props="{ expandTrigger: 'hover' }"
                       filterable
                       @change="handleChange"></el-cascader>
        </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="内容" prop="goodsDetails">
        <Tinymce v-model="form.goodsDetails" style="min-height: 300px;"></Tinymce>
      </el-form-item>
      <el-form-item>
        <el-button v-loading="saveLoading" element-loading-background="rgba(0, 0, 0, 0.3)" type="primary"
                   @click="handlerSubmit" v-has-permi="['admin:commodity:save']">保存
        </el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
    <el-dialog
        :visible.sync="uploads.show"
        title="上传封面"
        :append-to-body="true"
        destroy-on-close
        :close-on-click-modal="false"
        width="1000px">
      <UploadFile
          v-if="uploads.show"
          :multiple="uploads.multiple"
          @getFile="getFile"
          ref="uploads"
      />
    </el-dialog>
    <el-dialog
        :visible.sync="imgUploads.show"
        title="上传图片"
        :append-to-body="true"
        destroy-on-close
        :close-on-click-modal="false"
        width="1000px">
      <UploadFile
          v-if="imgUploads.show"
          :multiple="imgUploads.multiple"
          @getFile="getImage"
          ref="uploads"
      />
    </el-dialog>
  </div>
</template>
<script>
import {detail, save} from "@/api/shopping";
import {getForm} from "@/utils/utils";
import UploadFile from "@/components/File/UploadFile.vue";
import Tinymce from "@/components/Tinymce/index.vue";
import {optionsList} from "@/api/business";

export default {
  components: {Tinymce, UploadFile},
  props: {
    id: {
      default: null
    }
  },
  data() {
    return {
      activeName: "first",
      otherInfoVisible: false,
      form: {
        id: null,
        goodsFace: '',
        goodsDetails: '',
        status: 1,
        name: '',
        number: '',
        file: [],
        fileId: '',
        integral: '',
        areaId: '',
        cityId: '',
        value: [],
      },
      rules: {
        goodsFace: [{required: true, message: '请上传封面', trigger: ['blur']}],
        file: [{required: true, message: '请上传图片', trigger: ['blur']}],
        name: [{required: true, message: '请填写商品名称', trigger: ['blur']}],
        integral: [{required: true, message: '请填写积分', trigger: ['blur']}],
        number: [{required: true, message: '请填写库存', trigger: ['blur']}],
        goodsDetails: [{required: true, message: '请填写内容', trigger: ['blur']}],
        value: [{required: true, message: '请填选择所在区域', trigger: ['blur']}],
      },
      roles: [],
      saveLoading: false,
      formLoading: false,
      uploads: {
        show: false,
        multiple: false,
      },
      imgUploads: {
        show: false,
        multiple: true,
      },
      typeList: [{id: 1, data: '实物'}, {id: 2, data: '虚拟'}],
      isHotList: [{id: 1, data: '是'}, {id: 2, data: '否'}],
      managementList: [],
      imageUrl: '',
      value: [],
      options: [{
        value: '',
        label: '',
        children: [{
          value: '',
          label: '',
          children: [{
            value: '',
            label: ''
          }]
        }]
      }],
    }
  },
  mounted() {
    this.imageUrl = process.env.VUE_APP_IMAGE_URL
    this.getOptions();
    if (this.id) {
      this.getDetail();
    }
  },
  methods: {
    handleChange(value) {
      this.form.value = value
      this.form.areaId = value[2]
      this.form.cityId = value[1]
    },
    getOptions() {
      optionsList().then(res => {
        this.options = res
      })
    },
    handleClick(tab) {
      if (tab.name === 'second') {
        this.otherInfoVisible = true;
      } else {
        this.otherInfoVisible = false;
      }
    },
    getFile(fileList) {
      this.form.goodsFace = fileList[0].url
      this.uploads.show = false
    },
    getImage(fileList) {
      this.form.file = fileList
      this.imgUploads.show = false
      let ids = []
      this.form.file.forEach(item => {
        ids.push(item.id)
      })
      this.form.fileId = ids.join(',')
    },
    getDetail() {
      this.formLoading = true
      detail(this.id).then(res => {
        getForm(res, this.form);
      }).finally(() => {
        this.formLoading = false;
      })
    },
    handlerSubmit() {
      if (this.saveLoading || this.formLoading) {
        return;
      }
      this.$refs.form.validate(valid => {
        if (!valid) return;
        this.saveLoading = true;
        save(this.form).then(() => {
          this.$message.success('保存成功');
          this.close();
        }).finally(() => {
          this.saveLoading = false
        })
      })
    },
    close() {
      this.$emit('hideEditDialog')
    }
  }
}
</script>
<style lang="scss" scoped>
.col {
  width: 360px;
}

.img {
  width: 120px;
  height: 120px;
  margin-right: 10px;
}

.imgList {
  display: flex;
  flex-wrap: wrap;

  .img {
    width: 120px;
    height: 120px;
    margin-right: 10px;
  }
}
</style>
